<template>
  <el-dialog
      :title="!dataForm.hzId ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
<el-form-item label="患者姓名" prop="hzName">
  <el-input v-model="dataForm.hzName" placeholder="患者姓名"></el-input>

</el-form-item>
<el-form-item label="患者性别" prop="hzSex">
  <el-select placeholder="患者性别" v-model="dataForm.hzSex">
    <el-option label="男" :value="1"></el-option>
    <el-option label="女" :value="0"></el-option>
  </el-select>
</el-form-item>
<el-form-item label="出生日期" prop="hzBirthDate">
  <el-date-picker
    v-model="dataForm.hzBirthDate"
    type="date"
    placeholder="出生日期"/>
</el-form-item>
<el-form-item label="出生地" prop="hzBirthPlace">
  <el-input v-model="dataForm.hzBirthPlace" placeholder="出生地"></el-input>

</el-form-item>
<el-form-item label="卡号" prop="hzCard">
  <el-input v-model="dataForm.hzCard" placeholder="卡号"></el-input>

</el-form-item>
<el-form-item label="婚姻状态" prop="hzMarry">
  <el-input v-model="dataForm.hzMarry" placeholder="婚姻状态"></el-input>

</el-form-item>
<el-form-item label="过敏药物" prop="hzGm">
  <el-input v-model="dataForm.hzGm" placeholder="过敏药物"></el-input>

</el-form-item>
<el-form-item label="患者头像" prop="hzImg">
  <el-input v-model="dataForm.hzImg" placeholder="患者头像"></el-input>

</el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          hzId: 0,
          hzName: '',
          hzSex: '',
          hzBirthDate: '',
          hzBirthPlace: '',
          hzCard: '',
          hzMarry: '',
          hzGm: '',
          hzImg: ''
        },
        dataRule: {
          hzName: [
            { required: true, message: '患者姓名不能为空', trigger: 'blur' }
          ],
          hzSex: [
            { required: true, message: '患者性别不能为空', trigger: 'blur' }
          ],
          hzBirthDate: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          hzBirthPlace: [
            { required: true, message: '出生地不能为空', trigger: 'blur' }
          ],
          hzCard: [
            { required: true, message: '卡号不能为空', trigger: 'blur' }
          ],
          hzMarry: [
            { required: true, message: '婚姻状态不能为空', trigger: 'blur' }
          ],
          hzGm: [
            { required: true, message: '过敏药物不能为空', trigger: 'blur' }
          ],
          hzImg: [
            { required: true, message: '患者头像不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.hzId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.hzId) {
            this.$http({
              url: this.$http.adornUrl(`/hospital/hz/info/${this.dataForm.hzId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.hzName = data.hz.hzName
                this.dataForm.hzSex = data.hz.hzSex
                this.dataForm.hzBirthDate = data.hz.hzBirthDate
                this.dataForm.hzBirthPlace = data.hz.hzBirthPlace
                this.dataForm.hzCard = data.hz.hzCard
                this.dataForm.hzMarry = data.hz.hzMarry
                this.dataForm.hzGm = data.hz.hzGm
                this.dataForm.hzImg = data.hz.hzImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hospital/hz/${!this.dataForm.hzId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'hzId': this.dataForm.hzId || undefined,
                'hzName': this.dataForm.hzName,
                'hzSex': this.dataForm.hzSex,
                'hzBirthDate': this.dataForm.hzBirthDate,
                'hzBirthPlace': this.dataForm.hzBirthPlace,
                'hzCard': this.dataForm.hzCard,
                'hzMarry': this.dataForm.hzMarry,
                'hzGm': this.dataForm.hzGm,
                'hzImg': this.dataForm.hzImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
